package com.logos.socket;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class UDPListen {

    public static void main(String[] args) throws Exception {
        // 接收数据
        // 创建UDP接收端
        DatagramSocket ds = new DatagramSocket(10086);
        // 接收数据包
        DatagramPacket dp = new DatagramPacket(new byte[1024], 1024);
        // 该方法是阻塞的
        ds.receive(dp);
        // 解析数据包
        byte[] data = dp.getData();
        int length = dp.getLength();
        InetAddress address = dp.getAddress();
        // 输出数据
        System.out.println(new String(data, 0, length));
        System.out.println(address.getHostName());
        // 关闭资源
        ds.close();
    }
}
